# coding:utf-8
import csv
import datetime
import json
import os

import jsonpath
import requests
from retrying import retry


class Exam():
    # 创建session对象
    s = requests.Session()

    def __init__(self, url=None, headers=None):
        # 设置路径
        print('设置路径时，若需要设置递增，请在url中需要递增的位置用%s替换原内容')
        if url:
            if input('将该路径设为路径？(确定请输入1）：') == '1':
                self.url = url
            if input('是否需要设置递增？(确定请输入1）：') == '1':
                num, add = input('请输入初始值：'), input('请输入递增值：')
                if num.isalnum() and add.isalnum():
                    self.num, self.add = int(num), int(add)
                else:
                    print('请输入纯数字')
        else:
            self.num = None
            self.url = 'https://shuyang.anjuke.com/v3/ajax/map/sale/1814/prop_list/?room_num=-1&price_id=-1&area_id=-1&floor=-1&orientation=-1&is_two_years=0&is_school=0&is_metro=0&order_id=0&p=1&zoom=12&lat=33.91158_34.322823&lng=118.44822_119.172614&kw=&maxp=99&et=172796&ib=1&bst=pem203'
        # 设置headers
        if headers and type(headers) == dict:
            self.headers = headers
        else:
            self.headers = {
                'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36'}

    # 发送请求
    @retry(stop_max_attempt_number=3)
    def RTX(self, ip=None, post=None):
        try:
            if self.num:
                print('当前递增值',self.num)
                url = self.url % self.num
                self.num += self.add
            else:
                url = self.url
            # 设置代理的ip和post
            if ip and post:
                proxies = {"http": "http://%s:%s" % (ip, post), "https": "http://%s:%s" % (ip, post)}
                print('当前路径：%s\n当前headers:%s\n当前ip:%s' % (url, self.headers, proxies))
                self.r = self.s.get(url=url, headers=self.headers, timeout=5, proxies=proxies)
            else:
                # 发送请求
                print('当前路径：%s\n当前headers:%s\n当前ip:本机' % (url, self.headers))
                self.r = self.s.get(url=url, headers=self.headers, timeout=5, proxies=None)
        except Exception as err:
            print('响应出错 %s' % err)
            return False
        else:
            return True

    # 接收请求
    def RDX(self):
        # 获取json
        if 'text/html' in self.r.headers.get('Content-Type'):
            print('访问被禁止，请尝试更换ip后重试 返回类型为html，非json格式')
        else:
            self.ret = json.loads(self.r.content.decode())
            # 提取数据
            text = jsonpath.jsonpath(self.ret, '$.val.props')
            # 获取当前日期
            data = '%s%02d%02d' % (
                datetime.datetime.now().year, datetime.datetime.now().month, datetime.datetime.now().day)
            # 判断文件是否存在
            if os.path.exists('沭阳房产_%s.csv' % data):
                open_choose = 'a'
                print('打开文件...沭阳房产_%s.csv' % data)
            else:
                open_choose = 'w'
                print('新建文件...沭阳房产_%s.csv' % data)
            # 打开文件

            with open('沭阳房产_%s.csv' % data, '%s' % open_choose, encoding='utf-8') as f:
                file_write = csv.DictWriter(f, ['region_name', 'block_name', 'rhval', 'area', 'price', 'long_title',
                                                'comm_name',
                                                'house_orient_name', 'floor_tag'])
                if open_choose == 'w':
                    file_write.writeheader()
                for i in text[0]:
                    row = {'region_name': i.get('region_name'),
                           'block_name': i.get('block_name'),
                           'rhval': i.get('rhval'),
                           'area': i.get('area'),
                           'price': i.get('price'),
                           'long_title': i.get('long_title'),
                           'comm_name': i.get('comm_name'),
                           'house_orient_name': i.get('house_orient_name'),
                           'floor_tag': i.get('floor_tag')}
                    file_write.writerow(row)
            print('成功！')

    def change_url(self, new_url):
        if new_url and new_url.startswith('http://' or 'https://'):
            self.url = new_url

    def change_ip(self, ip, post):
        if ip and post:
            self.ip, self.port = ip, post


print('————————————————————创建对象——————————')
a = Exam(input('路径（可为空）：'), input('headers（可为空）：'))

for i in range(1,100):
    print('————————————————————第%s次尝试——————————' % i)
    if a.RTX():
        a.RDX()
    menu = input('————————————————————————————————————————\n1.停止程序 2.更换路径 3.更换ip （或直接按回车键继续）')
    if menu == '1':
        break
    elif menu == '2':
        a.change_url(input('请输入新路径：'))
    elif menu == '3':
        a.change_ip(input('请输入新ip：'), input('请输入新post：'))
